<?php
class Megadrupal_Slider_Block_Adminhtml_Slider_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
       
    /**
     * Load Wysiwyg on demand and Prepare layout
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
    }
    
    protected function _prepareForm()
    {

        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset("slider_form", array("legend"=>Mage::helper("slider")->__("Slide Config")));
        
        $fieldset->addField("title", "text", array(
            "label" => Mage::helper("slider")->__("Title"),
            "name" => "title"
        ));

        $fieldset->addField("description", "text", array(
            "label" => Mage::helper("slider")->__("Description"),
            "name" => "description"
        ));

        $fieldset->addField("machine_name", "text", array(
            "label" => Mage::helper("slider")->__("Machine Name"),
            "name" => "machine_name"
        ));
        $fieldset->addField('settings', 'hidden', array(
            'name'      => 'settings',
            'label'     => Mage::helper('cms')->__('Settings'),
            'title'     => Mage::helper('cms')->__('Settings')
        ));
        
        
        // variable to serialize string         
       $fieldset->addField('setting_full_width', 'checkboxes', array(
          "label" => Mage::helper("slider")->__("Full width"),
          "name" => 'setting[full_width]',
          'values' => array(array('value'=>'1','label'=>'')),
          'onclick' => "",
          'onchange' => "",
          'disabled' => false
        ));
        
        $fieldset->addField("setting_width", "text", array(
            "label" => Mage::helper("slider")->__("Width"),
            "name" => 'setting[width]'
        ));      
        $fieldset->addField("setting_height", "text", array(
            "label" => Mage::helper("slider")->__("Height"),
            "name" => 'setting[height]'
        ));         
        
        $fieldset->addField('setting_create_bg_imgstyle', 'checkboxes', array(
            "label" => Mage::helper("slider")->__("Create image style for slide background "),
            "name" => 'setting[create_bg_imgstyle]',
            'values' => array(array('value'=>'1','label'=>'')),
            'onclick' => "",
            'onchange' => "",
            'disabled' => false
        ));      
        
        $_animationItems = Mage::helper('slider')->getAnimationOption();
        $fieldset->addField("setting_animation", "select", array(
            "label" => Mage::helper("slider")->__("Slide animation"),
            "name" => 'setting[animation]',
            'values'    => $_animationItems
        ));         
              
       $fieldset->addField('setting_touch_swipe', 'checkboxes', array(
            "label" => Mage::helper("slider")->__("Enable touch swipe"),
            "name" => 'setting[touch_swipe]',
            'values' => array(array('value'=>'1','label'=>'')),
            'onclick' => "",
            'onchange' => "",
            'disabled' => false
        ));      
       $fieldset->addField('setting_responsive', 'checkboxes', array(
            "label" => Mage::helper("slider")->__("Enable responsive"),
            "name" => 'setting[responsive]',
            'values' => array(array('value'=>'1','label'=>'')),
            'onclick' => "",
            'onchange' => "",
            'disabled' => false
        ));        
        $fieldset->addField('setting_ldbar', 'radios', array(
            "label" => Mage::helper("slider")->__("Loading bar "),
            "name" => 'setting[ldbar]',
            'onclick' => "",
            'onchange' => "",
            'values' => array(
                              array('value'=>'bar','label'=>'Bar'),
                              array('value'=>'none','label'=>'None')
                         ),
            'disabled' => false,
            'readonly' => false
        ));
        
        $_barPosOptions = Mage::helper('slider')->getBarPosOption();
        $fieldset->addField("setting_bar_position", "select", array(
            "label" => Mage::helper("slider")->__("Bar position"),
            "name" => 'setting[bar_position]',
            'values'    => $_barPosOptions
        ));  
        
        $fieldset->addField('navigation', 'label', array(
            'label'     => Mage::helper('slider')->__('Navigation'),
            'style'     => 'font-size: 15px; font-weight: bold; line-height: 50px;'
        ));
        $fieldset->addField('setting_show_next_prev_button', 'checkboxes', array(
            "label" => Mage::helper("slider")->__("Show next previous button"),
            "name" => 'setting[show_next_prev_button]',
            'values' => array(array('value'=>'1','label'=>'')),
            'onclick' => "",
            'onchange' => "",
            'disabled' => false
        ));   
        $fieldset->addField('setting_auto_play', 'checkboxes', array(
            "label" => Mage::helper("slider")->__("Auto play"),
            "name" => 'setting[auto_play]',
            'values' => array(array('value'=>'1','label'=>'')),
            'onclick' => "",
            'onchange' => "",
            'disabled' => false
        ));     
        $fieldset->addField('setting_show_bullet', 'checkboxes', array(
            "label" => Mage::helper("slider")->__("Show bullet"),
            "name" => 'setting[show_bullet]',
            'values' => array(array('value'=>'1','label'=>'')),
            'onclick' => "",
            'onchange' => "",
            'disabled' => false
        ));   
        $_bulletPos = Mage::helper('slider')->getBulletPositon();
        $fieldset->addField("setting_bullet_position", "select", array(
            "label" => Mage::helper("slider")->__("Bullet position"),
            "name" => 'setting[bullet_position]',
            'values'    => $_bulletPos
        ));  
            
        $fieldset->addField('setting_show_thumbnail', 'checkboxes', array(
            "label" => Mage::helper("slider")->__("Show thumbnail"),
            "name" => 'setting[show_thumbnail]',
            'values' => array(array('value'=>'1','label'=>'')),
            'onclick' => "",
            'onchange' => "",
            'disabled' => false
        ));           
        
        $fieldset->addField("setting_thumb_width", "text", array(
            "label" => Mage::helper("slider")->__("Thumbnail width"),
            "name" => 'setting[thumb_width]'
        ));      
        $fieldset->addField("setting_thumb_height", "text", array(
            "label" => Mage::helper("slider")->__("Thumbnail height"),
            "name" => 'setting[thumb_height]'
        ));    
        
        
        $fieldset->addField("setting_delay", "text", array(
            "label" => Mage::helper("slider")->__("Slide delay"),
            "name" => 'setting[delay]'
        ));      
        $fieldset->addField("setting_transtime", "text", array(
            "label" => Mage::helper("slider")->__("Slide's translation time"),
            "name" => 'setting[transtime]'
        ));    
        
        if (Mage::getSingleton("adminhtml/session")->getSliderData())
        {
            $sliderData = Mage::getSingleton("adminhtml/session")->getSliderData(); 
            $this->_prepareSliderData($sliderData);
            
            $form->setValues($sliderData);
            Mage::getSingleton("adminhtml/session")->setSliderData(null);
        }elseif(Mage::registry("slider_data")) {
            $sliderData = Mage::registry("slider_data")->getData(); 
            $this->_prepareSliderData($sliderData);
            $form->setValues($sliderData);
        }

        return parent::_prepareForm();
    }
    
    public function _prepareSliderData(&$sliderData) {
        $_settings = unserialize($sliderData['settings']);
        $sliderData['setting'] = $_settings;
        foreach ($_settings as $skey => $_setting) {
            $skey = str_replace(array("'"), '', $skey);
            $sliderData['setting_'.$skey] = $_setting;
        }
    }
}
